package loggers;


import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDletStateChangeException;

import util.PictureItem;

import connectionSettings.BTSerialConnectionSettings;
import connectionSettings.ConnectionSettings;
import connections.BTSerialConnection;
import connections.Connection;
import connections.ConnectionListener;
import connections.Uploader;



public class LogDataFromBTConnection extends Logger implements  ConnectionListener{
	

	
	public BTSerialConnection btSensorConnection;


	
	public void startLogging() {
		connectionSettings = new BTSerialConnectionSettings(this);
	//	connectionSettings.setDelimitationStyle(ConnectionSettings.DELIMITATION_END_CHARACTER );
	//	connectionSettings.setEndChar(10);
		connectionSettings.displayMe(Display.getDisplay(this));
	
	}

	
	public void setUpSensorConnection() {
		btSensorConnection = (BTSerialConnection) connectionSettings.getConnection();
		connectionSettings.addListener(this);
		//btSensorConnection.addListener(this, (char) 13); // wait until you get the line return character (ascii 13)
		//btSensorConnection.send((byte) 13);
		// don't need this if we govern the frequency of sending on the sensor
		// or if the sensor is always sending and we only record after the interval has elapsed
		super.setUpSensorConnection();
	
	}
	public void takeDownSensorConnection(){
		// TODO Auto-generated method stub
		try{
			Connection myConnection = connectionSettings.getConnection();
			myConnection.disconnect();
		}catch(NullPointerException e){
			System.out.println("Never Connected");
		}
	
	}
	public void setUploaderProtocol(Uploader _uploader) {
		//_uploader.setAppend(true);
		//_uploader.setAllowBunching(true);
		//_uploader.setContentFormattingStyle(Uploader.TIMESTAMP_CONTENTS_METADATA_ASCII);
	}
	
	public void stopLogging() {
		 btSensorConnection.disconnect();
	}
	
	public void connectionMade(Connection _c, String _address) {
		System.out.println("Connection to Log Data from BT");
		//mainForm.deleteAll();

		//inputName  = btSensorConnection.getFriendlyName();
		displayInterface();
		Display.getDisplay(this).setCurrent(mainForm);
		setUpSensorConnection();

	}
	public void feedback(Connection _c, String _feedback) {
		mainForm.append(_feedback);
	}
	public void incoming(Connection _whichConnection, byte[] _input) {
		inputFromSensor(_input);
	}

	

}
